package upbeat;

import java.util.Vector;

/**
 * 
 * This class handles the input for the {@link Conductor} class.  It stores the data and provides a simple interface
 * for the Upbeat-programmer implemented version of Conduct to access it with.
 * 
 * @author Matt
 *
 */
public class DataHandler {
	private boolean mMore;
	private Vector<Double> mData;
	
	/**
	 * Default constructor.  Initializes mMore to true.  Need to add in initialization of
	 * data structure to be used to keep track of data...
	 */
	DataHandler() {
		mMore = true;
		mData = new Vector<Double>();
		mData.add( new Double(0.0) );
	}
	
	/**
	 * Used by the {@link Conductor} to give data to the InputHandler. 
	 * @param newData	the new data to be kept track of.
	 */
	void FeedData( double newData ) {
		Double data = new Double(newData);
		mData.add( data );
	}
	
	/**
	 * Gives the most recent input value.
	 * @return	the most recent input value.
	 */
	public double CurrentValue() {
		return mData.lastElement().doubleValue();
	}
	
	/**
	 * Gives the input that was received at a particular time.  Might want to
	 * make this throw an exception if a time in the future is specified.
	 * @param time	when was the input received
	 * @return		the input at time.
	 * @throws Exception 
	 */
	public double ValueAt( int time ) throws Exception {
		if( time >= 0 && time < mData.size() ) {
			return mData.elementAt(time).doubleValue();
		}
		String err = String.format("Error: DataHandler.ValueAt(): time %d is out of range!  Returning 0.0.", time);
		throw new IndexOutOfBoundsException(err);
	}
	
	/**
	 * Sets mMore.  If mMore == false, then that's the end of the song!
	 * @param more	The new value for mMore.
	 */
	void SetMore( boolean more ) {
		mMore = more;
	}
	
	/**
	 * Determines whether there will be more input forthcoming or not.
	 * @return	whether or not there will be more input forthcoming.
	 */
	public boolean GetMore() {
		return mMore;
	}
}
